package com.onenine.app.adapter;

import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.onenine.app.R;
import com.onenine.app.utils.CHelper;
import com.onenine.app.utils.GlideUtils;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.chat.model.LNewFriend;
import com.onenine.chat.model.LUser;
import com.onenine.chat.rongcloud.UserCacheMgr;
import com.onenine.chat.util.SimpleCommonUtils;
import com.onenine.common.util.ApUtils;
import com.onenine.common.util.TimeUtils;

import java.util.List;

public class GreetMsgAdapter extends BaseQuickAdapter<LNewFriend, BaseViewHolder> {

    private int avatarSize;

    public GreetMsgAdapter(@Nullable List<LNewFriend> data) {
        super(R.layout.layout_new_friend_item, data);
        avatarSize = ApUtils.getApp().getResources().getDimensionPixelSize(R.dimen.avatar_size_44dp);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final LNewFriend newFriend) {
        SimpleCommonUtils.spannableEmoticonFilter(holder
                .getView(R.id.tv_content), newFriend.getLastMessage());
        holder.getView(R.id.iv_bubble).setBackgroundResource(RoleResLoader
                .getRoleDrawableResourceId(RoleResLoader.DynamicResName.CIRCLE_POINT_BUBBLE));
        holder.setText(R.id.tv_time, TimeUtils.convertWXTimeStr(newFriend.getDatetime()))
                .setVisible(R.id.iv_bubble, !newFriend.isRead());
        View btnDel = holder.getView(R.id.btn_delete);
        btnDel.setBackgroundResource(RoleResLoader.getRoleDrawableResourceId(RoleResLoader.DynamicResName.THEME_GRADIENT_BG));
        UserCacheMgr.getInstance().getUserAsync(newFriend.getTargetUserId(), new UserCacheMgr.OnUserCallback() {
            @Override
            public void callback(LUser user) {
                holder.setText(R.id.tv_name, user.getNickName());
                GlideUtils.loadSrcImage(ApUtils.getApp(), holder.getView(R.id.iv_avatar)
                        , CHelper.getOssThumbImageUrl(user.getPhoto(), avatarSize));
            }
        });
        holder.addOnClickListener(R.id.rl_item);
        holder.addOnClickListener(R.id.btn_delete);
    }

}
